﻿Imports System.Data.SqlClient

Public Class adm_editcolumnnew
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private col As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)
    Dim strErr As String = ""
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strCID As String
        Dim pId As String
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            strCID = Trim(Request("newsId"))
            ps.addParameter("id", strCID)
            If (Request("subcolumnnews") <> Nothing And Request("subcolumnnews") <> "") Then
                result.InnerHtml = ""
                If strTitle.Text = "" Then

                    result.InnerHtml = "<center><b><font color=""#FF0000""> Please enter your news title</font></b></center>"
                    Exit Sub
                End If
                If columnnews.InnerText = "" Then

                    result.InnerHtml = "<center><b><font color=""#FF0000""> Please enter your news content</font></b></center>"
                    Exit Sub
                End If
                If Request("dojo_date") = "" Then

                    result.InnerHtml = "<center><b><font color=""#FF0000""> Please choose your News date</font></b></center>"
                    Exit Sub
                End If
                If column.SelectedValue = "0" Then
                    result.InnerHtml = "<center><b><font color=""#FF0000""> Please choose your news column</font></b></center>"
                    Exit Sub
                End If

                sSQL = "update Column_New set title=@title,context=@context,Column_ID=@column,news_date=@newsDate"
                sSQL = sSQL & " where New_ID=@id"
                ps.strSql = sSQL
                ps.addParameter("column", column.SelectedValue)
                ps.addParameter("title", strTitle.Text)
                ps.addParameter("context", Request("c_content"))
                ps.addParameter("newsDate", Request("dojo_date"))
                objfind.es(ps, strErr)
                Dim newsId, fileFullName, fileName, photoPath As String
                newsId = strCID
                If (image.Visible) Then
                    If (image.PostedFile.FileName <> Nothing) Then
                        pId = System.Guid.NewGuid().ToString()
                        fileFullName = image.PostedFile.FileName
                        fileName = fileFullName.Substring(fileFullName.LastIndexOf(".") + 1)
                        System.IO.Directory.CreateDirectory(Server.MapPath("../../") & "pic/news/" & newsId)
                        image.PostedFile.SaveAs(Server.MapPath("../../") & "pic/news/" & newsId & "/" & pId & "." & fileName)
                        photoPath = "/pic/news/" & newsId & "/" & pId & "." & fileName
                        ps.strSql = "update Column_New set photo_path = @photoPath where New_ID=@id"
                        ps.addParameter("photoPath", photoPath)
                        objfind.es(ps, strErr)
                        Me.news_id.Value = newsId
                        imageDiv.InnerHtml = "<img src=""" & photoPath & """/>"
                    End If
                End If
                If strErr = Nothing Or strErr = "" Then
                    result.InnerHtml = ""
                    result.InnerHtml = "<center><b><font color=""#FF0000""> Update Success！</font></b></center>"
                Else
                    result.InnerHtml = ""
                    result.InnerHtml = "<center><b><font color=""#FF0000"">Error！</font></b><p>" & strErr & "</p></center>"
                End If
            End If
            sSQL = "select * from Column_New where New_ID=@id"
            ps.strSql = sSQL
            Dim conn As SqlConnection = objfind.getConn()
            objrd = objfind.dr(ps, strErr, conn, objrd)
            If objrd.Read() Then
                strTitle.Text = objrd("title").ToString

                columnnews.InnerHtml = objrd("context").ToString
                c_date.Value = Year(objrd("news_date")) & "-" & Format(Month(objrd("news_date")), "00") & "-" & Format(Day(objrd("news_date")), "00")
                col = objrd("Column_ID").ToString
                imageDiv.InnerHtml = "<img src=""" & objrd("photo_path") & """  />"
            End If
            sSQL = "select * from Columns"
            ps.strSql = sSQL
            objrd = objfind.dr(ps, strErr, conn, objrd)
            While (objrd.Read())
                column.Items.Add(New ListItem(
                 objrd("Column_Name").ToString(), objrd("Column_ID").ToString()))
            End While
            column.Items.Add(New ListItem(
                 "未分类", "0"))

            column.SelectedValue = col

            objfind.closeConn(conn)
        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub
    

    Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("adm_columnnews.aspx")
    End Sub
End Class